package com.ys.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ys.common.ApiRestResponse;
import com.ys.model.pojo.Maintain;
import com.ys.model.request.RepairSearchReq;
import com.ys.model.request.UpdateMaintainReq;
import com.ys.model.vo.MaintainDetailVo;
import com.ys.model.vo.SearchVo;
import com.ys.service.ComponentService;
import com.ys.service.EquipmentService;
import com.ys.service.MaintainService;
import com.ys.service.PictureService;
import com.ys.utils.ImgToBase64;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/maintain")
public class MaintainController {
    @Autowired
    ComponentService componentService;
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    PictureService pictureService;
    @Autowired
    MaintainService maintainService;
    @Value("${file.path}")
    private String pictureUrl;

    @ApiOperation(value = "维护更新",notes = "mId:维护id(int)mPeopleName:维护人员(String)mTime:维护时间 (require)mContent:维护内容(String)eId:设备id(String)")
    @PostMapping("/update")
    @ResponseBody
    public ApiRestResponse maintainUpdate(@Valid @RequestBody UpdateMaintainReq updateMaintainReq)
    {
        Maintain maintain = new Maintain();
        BeanUtils.copyProperties(updateMaintainReq,maintain);
        maintainService.updateMaintain(maintain);
        return ApiRestResponse.success();
    }

//    *********************************************

    /*维护查询列表*/
    @GetMapping("/searchList")
    @ApiOperation("维护查询下拉列表")
    public ApiRestResponse maintainedSearchList(){
        List<SearchVo> equipmentSearchList = equipmentService.equipmentSearchList();
        List<SearchVo> componentSearchList = componentService.allCName();
        //将机台、设备等对应放进一个list集合
        List<String> eWorkshops = new ArrayList<>();
        List<String> eMachines =  new ArrayList<>();
        List<String> eNames =  new ArrayList<>();
        for(SearchVo searchVo : equipmentSearchList){
            eWorkshops.add(searchVo.geteWorkshop());
            eMachines.add(searchVo.geteMachine());
            eNames.add(searchVo.geteName());
        }

        List<String> cNames =  new ArrayList<>();
        for(SearchVo searchVo : componentSearchList){
            cNames.add(searchVo.getcName());
        }

        JSONObject object1 = new JSONObject();
        object1.put("eWorkshops",eWorkshops);
        object1.put("eMachines",eMachines);
        object1.put("eNames",eNames);
        object1.put("cNames",cNames);
        return new ApiRestResponse().success(object1);
    }

    @PostMapping("/allMaintainList")
    @ApiOperation("维护分页查询功能：如果查询字段全部为空，就返回全部的列表，否则返回符合条件的列表")
    public ApiRestResponse search(@Valid @RequestBody RepairSearchReq repairSearchReq){

        PageHelper.startPage(repairSearchReq.getPageNum(), repairSearchReq.getPageSize());
        List<MaintainDetailVo> maintainList = maintainService.search(repairSearchReq);
        /*for (MaintainDetailVo maintainDetailVo:maintainList) {
            int mId = maintainDetailVo.getmId();
            List<String> picturesName = pictureService.getPNameByMid(mId);
            List<String> picURL = new ArrayList<>();
            for (String string:picturesName) {
                String name = "file:///"+pictureUrl+"/"+string;
                string = ImgToBase64.requestUrlToBase64(name);
                picURL.add(string);
            }
            maintainDetailVo.setpName(picURL);
        }*/

        PageInfo p = new PageInfo(maintainList);
        return new ApiRestResponse().success(p);
    }

    @PostMapping("/getPNameByMid")
    @ApiOperation("根据mId得到图片")
    public ApiRestResponse getPNameByMid(@Valid @RequestBody RepairSearchReq repairSearchReq){
            List<String> picturesName = pictureService.getPNameByMid(repairSearchReq.getmId());
            if(!picturesName.isEmpty()){
                List<String> picURL = new ArrayList<>();
                for (String string:picturesName) {
                    String name = "file:///"+pictureUrl+"/"+string;
                    string = ImgToBase64.requestUrlToBase64(name);
                    picURL.add(string);
                }
                return new ApiRestResponse().success(picURL);
            }else{
                return new ApiRestResponse().error(2000,"该维修记录无图片！");            }



    }

    @ApiOperation("删除维护数据")
    @PostMapping("/deleteMaintain")
    public ApiRestResponse deleteMaintainById(@Valid @RequestBody Maintain maintain){
        if(maintain.getmId()==null){
            return new ApiRestResponse().error(2000,"id为空");
        }else{
            maintainService.deleteMaintainById(maintain.getmId());
            return new ApiRestResponse().success();
        }

    }




}
